template <int NumOff, typename In, typename Out, typename F>
void stencil(int n, const In a[], Out r[], In b, F func, const int offsets[]) {
  In neighborhood[NumOff];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < NumOff; ++j) {
      int k = i + offsets[j];
      if (0 <= k && k < n) {
        neighborhood[j] = a[k];
      } else {
        neighborhood[j] = b;
      }
    }
    r[i] = func(neighborhood);
  }
}